cc.Class({
    extends: cc.Component,

    properties: {
        dian:cc.Node,
    },

    // use this for initialization
    onLoad: function () {
        // for(var i = 0; i < this.dian.children.length;++i){
        //     this.dian.children[i].active = false;
        // }
        // this.initMgr();
        // this.onLoadComplete();
        var self=this;
        this.node.on("init_login_push",function(date){
            console.log("data of LoadingLogic.js="+date);
        });

        this.node.on("enter_login_push",function(data){
            console.log("ok enter login ");
            self.confirmClick();
        });

        if (!cc.sys.isNative) {
            this.confirmClick();
        }
    },

    confirmClick:function(){
        for(var i = 0; i < this.dian.children.length;++i){
            this.dian.children[i].active = false;
        }
        this.initMgr();
        this.checkVersion();

    },
    
    initMgr:function(){
        cc.vv = {};
        cc.vv.http_isConnect=true;
        cc.vv.net_isConnect=false;
        // console.log('require("UserMgr")');
        var UserMgr = require("UserMgr");
        cc.vv.userMgr = new UserMgr();
        // console.log('new UserMgr()');
        
        var ReplayMgr = require("ReplayMgr");
        cc.vv.replayMgr = new ReplayMgr();
        
        cc.vv.http = require("HTTP");
        cc.vv.global = require("Global");
        cc.vv.net = require("Net");

        cc.vv.initVersion=require("InitVersion");
        cc.vv.initVersion.init();
        cc.vv.initVersion.sayHi();
        
        var GameNetMgr = require("GameNetMgr");
        cc.vv.gameNetMgr = new GameNetMgr();
        cc.vv.gameNetMgr.initHandlers();
        
        var AnysdkMgr = require("AnysdkMgr");
        cc.vv.anysdkMgr = new AnysdkMgr();
        cc.vv.anysdkMgr.init();
        
        var VoiceMgr = require("VoiceMgr");
        cc.vv.voiceMgr = new VoiceMgr();
        cc.vv.voiceMgr.init();
        
        var AudioMgr = require("AudioMgr");
        cc.vv.audioMgr = new AudioMgr();
        cc.vv.audioMgr.init();
        
        var Utils = require("Utils");
        cc.vv.utils = new Utils();

        var SetPeople=require("SetPeople");
        cc.vv.setPeople=new SetPeople();

        cc.vv.timeout=require("CheckTimeout");
        
        cc.args = this.urlParse();

        cc.vv.audioMgr.playBGM("bgMain.mp3");
    },

    checkVersion:function(){
        var self = this;
        var onGetVersion = function(ret){
            // console.log("ret.version="+ret.version);
            cc.vv.SI=ret;
            self.startPreloading();
        };
        var devi =cc.sys.os == cc.sys.OS_ANDROID?"android":(cc.sys.os == cc.sys.OS_IOS?"ios":"other");
        cc.vv.http.sendRequest("/get_serverinfo",{device:devi},onGetVersion);
        
    },

    startPreloading:function(){
        var self = this;
        console.log("startPreloading of LoadingLogic");
        
        //用于检查是否已有微信账号
        if(cc.sys.os == cc.sys.OS_IOS){
            var account =  cc.sys.localStorage.getItem("wx_account");
            var sign = cc.sys.localStorage.getItem("wx_sign");
            cc.log(account,sign);

            if(!account||!sign){
                cc.log("not account or sign");
                self.onLoadComplete();
                return;
            }
        }

        cc.loader.loadResDir("textures", function (err, assets) {
            self.onLoadComplete();
        });
        
        cc.loader.onProgress = function ( completedCount, totalCount,  item ){
        };
        
        cc.loader.onComplete = function(error, items){
            self.onLoadComplete();
        }        
    },
    
    urlParse:function(){
        var params = {};
        if(window.location == null){
            return params;
        }
        var name,value; 
        var str=window.location.href; //取得整个地址栏
        var num=str.indexOf("?") 
        str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
        
        var arr=str.split("&"); //各个参数放到数组里
        for(var i=0;i < arr.length;i++){ 
            num=arr[i].indexOf("="); 
            if(num>0){ 
                name=arr[i].substring(0,num);
                value=arr[i].substr(num+1);
                params[name]=value;
            } 
        }
        return params;
    },
    
    onLoadComplete:function(){
        console.log("onLoadComplete of LoadingLogic.js")
        cc.director.loadScene("login");
    },

    update: function (dt) {
        for(var i = 0; i < this.dian.children.length;++i){
            this.dian.children[i].active = false;
        }
        var t = Math.floor(Date.now() / 1000) % this.dian.children.length;
        this.dian.children[t].active = true;
    }
});